"""
Importable Illustrious CSV-to-Dict Converter
--------------------------------------------

This module provides `convert_csv_to_py` for use by scripts and wrappers.
Prefer importing this module from other Python code.
"""

import csv


def convert_csv_to_py(input_path: str, output_path: str, var_name: str) -> None:
    mapping: dict[str, str] = {}
    with open(input_path, newline="", encoding="utf-8") as f:
        reader = csv.reader(f)
        rows = list(reader)

    # If header present, detect and skip
    if rows and any(cell.lower() in {"key", "name"} for cell in rows[0]):
        rows = rows[1:]

    for row in rows:
        if not row or len(row) < 2:
            continue
        key = row[0].strip()
        val = row[1].strip()
        if key and val:
            mapping[key] = val

    with open(output_path, "w", encoding="utf-8") as out:
        out.write("# Auto-generated by illustrious_list_convert.py\n")
        out.write(f"{var_name} = {mapping!r}\n")


__all__ = ["convert_csv_to_py"]
